package com.ovft.cat.starter.aop;

import com.dianping.cat.Cat;
import com.dianping.cat.message.Transaction;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

@Aspect
@Component
public class CatAop {

    @Around(value = "execution(* *.*(..))")
    public void aroundMethod(ProceedingJoinPoint pjp) {
        MethodSignature joinPointObject = (MethodSignature) pjp.getSignature();
        Method method = joinPointObject.getMethod();

        if (true) {
            Transaction t = Cat.newTransaction("method", method.getName());

            try {
                pjp.proceed();
                t.setStatus(Transaction.SUCCESS);
                t.complete();
            } catch (Throwable e) {
                t.setStatus(e);
                Cat.logError(e);
            } finally {
                t.complete();
            }
        } else {
            try {
                pjp.proceed();
            } catch (Throwable e) {
            }
        }
    }


}
